import Action from "@/extra/action";
import { useActionManager, useBattle } from "../use";
import { Character, CharacterData } from "@/game/Character";
import emitter from "@/utils/events";
import { add, cloneDeep, concat, isNil, random, sum } from "lodash-es";
import { customAlphabet } from "nanoid";
const nanoid = customAlphabet('1234567890', 10)
export type ClientAction = Omit<Action, 'index'>

export async function addAction(action: ClientAction, send: boolean = true) {
  const battle = useBattle()
  const actionManager = useActionManager()
  const newAction: Action = { ...action, index: actionManager.value.length + 1 }
  await emitter.emitSerial('beforeAction', [action.name, action, { send }])
  actionManager.value.addAction(newAction)
  if (actionManager.value.isPlaying) {
    // actionManager.value.pending.push(newAction)
  } else {
    await actionManager.value.playAction()
  }
  await emitter.emitSerial('afterAction', [action.name, action, { send }])
}

/** 攻击 */
addAction.attack = async function ( enemy: Character,fightcard: Character | undefined) {
  if (!fightcard) return
  const damge = random(...enemy.temppro.atk)
  await addAction({
    name: 'attack',
    obj: fightcard.oriData,
    sub: enemy.oriData,
    para: { damge }
  })
}
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       


addAction.addCurrentHP = async function (fightcard: Character | undefined, HP: number) {
  if (!fightcard) return
  fightcard.currentHP += HP
  await addAction({
    name: 'addHP',
    obj: fightcard.oriData
  })
}